/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.materialMedia.constitutiveModel;

import java.io.Serializable;
import java.util.HashMap;

import br.ufmg.dees.insane.analysisModel.AnalysisModel;
import br.ufmg.dees.insane.analysisModel.EulerSpaceFrame;
import br.ufmg.dees.insane.analysisModel.KirchhoffPlate;
import br.ufmg.dees.insane.analysisModel.PlaneStrain;
import br.ufmg.dees.insane.analysisModel.PlaneStress;
import br.ufmg.dees.insane.analysisModel.PolarPlaneStrain;
import br.ufmg.dees.insane.analysisModel.PolarPlaneStress;
import br.ufmg.dees.insane.analysisModel.ReissnerMindlinPlate;
import br.ufmg.dees.insane.analysisModel.TimoSpaceFrame;
import br.ufmg.dees.insane.continuousPointModel.ContinuousPointModel;
import br.ufmg.dees.insane.continuousPointModel.KirchhoffPlatePoint;
import br.ufmg.dees.insane.continuousPointModel.PolarPlaneStressPoint;
import br.ufmg.dees.insane.continuousPointModel.ReissnerMindlinPlatePoint;
import br.ufmg.dees.insane.materialMedia.material.LinearElasticIsotropic;
import br.ufmg.dees.insane.materialMedia.material.LinearElasticOrthotropic;
import br.ufmg.dees.insane.materialMedia.material.Material;
import br.ufmg.dees.insane.materialMedia.material.PolarLinearElasticIsotropic;
import br.ufmg.dees.insane.util.IMatrix;
import br.ufmg.dees.insane.util.IVector;

/**
 * Class representing LinearElasticConstModel
 * @author roque
 * @since Nov 17, 2005
 */
public class LinearElasticConstModel implements ConstitutiveModel ,Serializable {
	 
	private static final long serialVersionUID = 1L;

	/**Constructs this Constitutive Model.*/
    public LinearElasticConstModel()
	{
	}
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#getLabel()
     */
    public String getLabel() {
        return "LinearElasticConstModel";
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#mountC(br.ufmg.dees.insane.analysisModel.AnalysisModel, br.ufmg.dees.insane.materialMedia.material.Material, java.util.HashMap)
     */
    public IMatrix mountC(AnalysisModel am, Material mat, HashMap cv) {
    	this.check(am,mat);
        return am.getDualInternalVariableOperator(mat.getPs());
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#mountC(br.ufmg.dees.insane.analysisModel.AnalysisModel, br.ufmg.dees.insane.materialMedia.material.Material, java.util.HashMap)
     */
    public IMatrix mountC(ContinuousPointModel am, Material mat, HashMap cv) {
    	this.check(am,mat);
        return am.getDualInternalVariableOperator(mat.getPs());
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#mountCt(br.ufmg.dees.insane.analysisModel.AnalysisModel, br.ufmg.dees.insane.materialMedia.material.Material, java.util.HashMap)
     */
    public IMatrix mountCt(AnalysisModel am, Material mat, HashMap cv) {
    	this.check(am,mat);
    	return am.getDualInternalVariableOperator(mat.getPs());
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#mountCt(br.ufmg.dees.insane.analysisModel.AnalysisModel, br.ufmg.dees.insane.materialMedia.material.Material, java.util.HashMap)
     */
    public IMatrix mountCt(ContinuousPointModel am, Material mat, HashMap cv) {
    	this.check(am,mat);
    	return am.getDualInternalVariableOperator(mat.getPs());
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#mountCs(br.ufmg.dees.insane.analysisModel.AnalysisModel, br.ufmg.dees.insane.materialMedia.material.Material, java.util.HashMap)
     */
    public IMatrix mountCs(AnalysisModel am, Material mat, HashMap cv) {
    	this.check(am,mat);
    	return am.getDualInternalVariableOperator(mat.getPs());
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#mountCs(br.ufmg.dees.insane.analysisModel.AnalysisModel, br.ufmg.dees.insane.materialMedia.material.Material, java.util.HashMap)
     */
    public IMatrix mountCs(ContinuousPointModel am, Material mat, HashMap cv) {
    	this.check(am,mat);
    	return am.getDualInternalVariableOperator(mat.getPs());
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#mountDualInternalVariableVector(br.ufmg.dees.insane.util.IVector, br.ufmg.dees.insane.analysisModel.AnalysisModel, br.ufmg.dees.insane.materialMedia.material.Material, java.util.HashMap)
     */
    public IVector mountDualInternalVariableVector(IVector e, AnalysisModel am, Material mat, HashMap cv) {
    	this.check(am,mat);
        IVector s = new IVector(e.getSize());
        s.mul(this.mountCs(am, mat, cv), e);
        return s;
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#mountDualInternalVariableVector(br.ufmg.dees.insane.util.IVector, br.ufmg.dees.insane.analysisModel.AnalysisModel, br.ufmg.dees.insane.materialMedia.material.Material, java.util.HashMap)
     */
    public IVector mountDualInternalVariableVector(IVector e, ContinuousPointModel am, Material mat, HashMap cv) {
    	this.check(am,mat);
        IVector s = new IVector(e.getSize());
        s.mul(this.mountCs(am, mat, cv), e);
        return s;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#check(br.ufmg.dees.insane.analysisModel.AnalysisModel)
     */
    public void check(AnalysisModel am) throws AnalysisModelNotValid {
        if (!(am instanceof PlaneStrain ||
            am instanceof PlaneStress ||
            am instanceof ReissnerMindlinPlate ||
         //   am instanceof Heterosis ||
            am instanceof KirchhoffPlate ||
            am instanceof PolarPlaneStrain ||
            am instanceof PolarPlaneStress ||
            am instanceof TimoSpaceFrame ||
            am instanceof EulerSpaceFrame )) {
                throw new AnalysisModelNotValid(this.getLabel()+" and "+am.getLabel());
            }
     }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#check(br.ufmg.dees.insane.analysisModel.AnalysisModel)
     */
    public void check(ContinuousPointModel am) throws ContinuousPointModelNotValid {
        if (!(am instanceof ReissnerMindlinPlatePoint ||
            am instanceof KirchhoffPlatePoint ||
            am instanceof PolarPlaneStressPoint )) {
                throw new ContinuousPointModelNotValid(this.getLabel()+" and "+am.getLabel());
            }
     }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#check(br.ufmg.dees.insane.materialMedia.material.Material)
     */
    public void check(Material mat) throws MaterialNotValid {
        if (!(mat instanceof LinearElasticIsotropic || 
                mat instanceof LinearElasticOrthotropic ||
                	mat instanceof PolarLinearElasticIsotropic)) {
            throw new MaterialNotValid(this.getLabel()+" and "+mat.getClass());
        }
     }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#update(java.util.HashMap, java.util.HashMap)
     */
    public void update(HashMap previous, HashMap vm) {
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#init(java.util.HashMap, java.util.HashMap, br.ufmg.dees.insane.analysisModel.AnalysisModel, br.ufmg.dees.insane.materialMedia.material.Material)
     */
    public void init(HashMap previous, HashMap cv, AnalysisModel am, Material mat) {
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#init(java.util.HashMap, java.util.HashMap, br.ufmg.dees.insane.analysisModel.AnalysisModel, br.ufmg.dees.insane.materialMedia.material.Material)
     */
    public void init(HashMap previous, HashMap cv, ContinuousPointModel am, Material mat) {
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#getNumberOfVariables()
     */
    public int getNumberOfVariables() {
        return 1;
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.constitutiveModel.ConstitutiveModel#getNumberOfPreviousVariables()
     */
    public int getNumberOfPreviousVariables() {
        return 0;
    }
    
    private void check(AnalysisModel am, Material mat){
    	try {
			this.check(am);
			this.check(mat);
		} catch (AnalysisModelNotValid e) {
			e.printStackTrace();
            System.exit(0);
		}catch (MaterialNotValid e) {
			e.printStackTrace();
            System.exit(0);
		}
    }
    
    private void check(ContinuousPointModel am, Material mat){
    	try {
			this.check(am);
			this.check(mat);
		} catch (ContinuousPointModelNotValid e) {
			e.printStackTrace();
            System.exit(0);
		}catch (MaterialNotValid e) {
			e.printStackTrace();
            System.exit(0);
		}
    }

}
